package cn.app.edu.projectpracticeofpracticaltraining.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.app.edu.projectpracticeofpracticaltraining.R;
import cn.app.edu.projectpracticeofpracticaltraining.adapter.SystemFragmentAdapter;
import cn.app.edu.projectpracticeofpracticaltraining.constant.ApiInterface;
import cn.app.edu.projectpracticeofpracticaltraining.entity.SystemEntity;
import cn.app.edu.projectpracticeofpracticaltraining.util.HandlerUtil;
import cn.app.edu.projectpracticeofpracticaltraining.util.OkHttpUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class SystemFragment extends Fragment {


    private TextView mTitle;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private Context context;
    private List<SystemEntity.DataBean> mList = new ArrayList<>();
    private SystemFragmentAdapter systemFragmentAdapter;

    public static SystemFragment newInstance() {

        Bundle args = new Bundle();

        SystemFragment fragment = new SystemFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_system, null);

        recyclerView = inflate.findViewById(R.id.rv_system_fragment_system);
        swipeRefreshLayout = inflate.findViewById(R.id.srl_fresh_fragment_system);

        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        context = getContext();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));


        systemFragmentAdapter = new SystemFragmentAdapter(context, mList, new SystemFragmentAdapter.OnClickListener() {
            @Override
            public void setOnClickListener(View view, int position, SystemEntity.DataBean dataBean) {
                Toast.makeText(context, dataBean.getName(),Toast.LENGTH_SHORT).show();
            }
        });

        recyclerView.setAdapter(systemFragmentAdapter);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getSystem();
            }
        });


        getSystem();
    }


    /****************************************网络请求************************************************/


    private void getSystem() {
        OkHttpUtil.enqueue(ApiInterface.SYSTEM_URL, null, null, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                HandlerUtil.post(new Runnable() {
                    @Override
                    public void run() {
                        new AlertDialog.Builder(context)
                                .setTitle("警告")
                                .setMessage("你的网络似乎有问题，请检查！")
                                .setPositiveButton("确定", null)
                                .show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                ResponseBody body = response.body();
                final String str = body.string();
                final SystemEntity systemEntity = new Gson().fromJson(str, SystemEntity.class);

                HandlerUtil.post(new Runnable() {
                    @Override
                    public void run() {
                        if (systemEntity.getErrorCode() == 0) {

                            mList.clear();
                            mList.addAll(systemEntity.getData());
                            systemFragmentAdapter.notifyDataSetChanged();
                            swipeRefreshLayout.setRefreshing(false);

                        }
                    }
                });


            }
        });
    }


}
